package com.lemner.www.hongyu.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.lemner.www.hongyu.Bean.FadanBean;
import com.lemner.www.hongyu.Bean.GetdepartmentBean;
import com.lemner.www.hongyu.Bean.Userbean;
import com.lemner.www.hongyu.Fragement.OrderFragment;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;


import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FadanActvivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_cause;
    private TextView tv_department;
    private Button btn_confirm;
    private ImageView iv_uppic;
    private Userbean.DataBean date;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    private OptionsPickerView PvDepartment;//部门选择器
    private ArrayList<GetdepartmentBean.DataBean.ListBean> options1Items = new ArrayList<>();//部门
    private String tx=null;
    private int worder_id,community_id,department_id;


    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fadan);
        date= (Userbean.DataBean) getIntent().getSerializableExtra("date");
        worder_id=date.getUser().getWorker_id();
        community_id=date.getUser().getCommunity_data().get(0).getCommunity_id();
        InitView();
    }

public void InitView(){
        et_cause=(EditText)findViewById(R.id.et_cause);
        tv_department=(TextView)findViewById(R.id.tv_dapartment);
        btn_confirm=(Button)findViewById(R.id.btn_confirm);
        iv_uppic=(ImageView)findViewById(R.id.iv_uppic);
        tv_department.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);
        iv_uppic.setOnClickListener(this);
    }

@Override
public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_uppic:
                break;
            case R.id.tv_dapartment:
                Department();
                break;
            case R.id.btn_confirm:
                String cause1=et_cause.getText().toString().trim();
                if(TextUtils.isEmpty(cause1))
                    Toast.makeText(getApplicationContext(),"问题详情不能为空",Toast.LENGTH_SHORT).show();
                if(TextUtils.isEmpty(tx))
                    Toast.makeText(getApplicationContext(),"请选择部门",Toast.LENGTH_SHORT).show();
                else
                SentOrder();
                break;
            default:
                break;
        }
    }

public void Selectdepartment(){
   PvDepartment = new  OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
            tx = options1Items.get(options1).getDepartment_name();
            department_id = options1Items.get(options1).getDepartment_id();
            tv_department.setText(tx);
        }
    })
           .setSubmitText("确定")//确定按钮文字
           .setCancelText("取消")//取消按钮文字
           .setTitleText("部门选择")
           .setSubCalSize(18)//确定和取消文字大小
           .setSubmitColor(Color.BLUE)//确定按钮文字颜色
           .setCancelColor(Color.BLUE)//取消按钮文字颜色
           .setBgColor(0xFFFFFFFF)//滚轮背景颜色 Night mode
           .setContentTextSize(18)//滚轮文字大小
           .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
           .setCyclic(false, false, false)//循环与否
           .setSelectOptions(1, 1, 1)  //设置默认选中项
           .setOutSideCancelable(false)//点击外部dismiss default true
           .isDialog(true)//是否显示为对话框样式
           .build();
    PvDepartment.setPicker(options1Items);
}
//获取部门信息
public void Department(){
       if (options1Items.size() != 0) {
        options1Items.clear();
    }
       Retrofit retrofit=new Retrofit.Builder()
               .baseUrl(BASEURL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();
      service=retrofit.create(Service.class);
       Call<GetdepartmentBean>call=service.getDepartment(date.getUser().getCommunity_data().get(0).getCommunity_id());
       call.enqueue(new Callback<GetdepartmentBean>() {
           @Override
           public void onResponse(Call<GetdepartmentBean> call, Response<GetdepartmentBean> response) {
                   options1Items.addAll(response.body().getData().getList());
                   Selectdepartment();
                   PvDepartment.show();
           }
           @Override
           public void onFailure(Call<GetdepartmentBean> call, Throwable t) {
               Log.e("error", t.getMessage());
           }
       });
   }
//发单
   public void SentOrder(){
       String cause=et_cause.getText().toString().trim();
       Retrofit retrofit1=new Retrofit.Builder()
               .baseUrl(BASEURL)
               .addConverterFactory(GsonConverterFactory.create())
               .build();
      Service service=retrofit1.create(Service.class);
       Call<FadanBean>call=service.getFadan(worder_id,department_id,community_id,cause,null);
       call.enqueue(new Callback<FadanBean>() {
           @Override
           public void onResponse(Call<FadanBean> call, Response<FadanBean> response) {
               if(response.body().getData().getSuccess()==1) {
                   Toast.makeText(getApplicationContext(), "发单成功", Toast.LENGTH_SHORT).show();
                   Intent intent = new Intent(FadanActvivity.this,MainActivity.class);
                   intent.putExtra("date",date);
                   startActivity(intent);
               }
               else
                   Toast.makeText(getApplicationContext(),"发单失败",Toast.LENGTH_SHORT).show();
           }
           @Override
           public void onFailure(Call<FadanBean> call, Throwable t) {
               Toast.makeText(getApplicationContext(),"发单失败",Toast.LENGTH_SHORT).show();
           }
       });
       }


   }


